package cn.edu.zufe.mjt;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 蒙锦涛
 * @date 2025/5/29
 * @description ReentrantLock的简单使用
 */


public class ReentrantLockTest {

    private volatile int num;

    private Lock lock = new ReentrantLock();

    private int getNum() {
        return num;
    }

    private void addNum() {
        lock.lock();
        try {
            Thread.sleep(5L);
            num++;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }


    public static void main(String[] args) throws InterruptedException {
        ReentrantLockTest r = new ReentrantLockTest();

        long start = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            new Thread(r::addNum).start();
        }


        Thread.sleep(3000L);
        long end = System.currentTimeMillis();

        System.out.println(r.getNum());
        System.out.println("耗时：" + (end - start) + "ms");
    }























}
